<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" append-to-body width="400px" @close='close' top="20vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" label-width="90px" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="type" prop="ip">
          <el-input v-emoji v-model="record.ip" ref="ip" size='small' style="width:217px;"></el-input>
        </el-form-item>
        <el-form-item :label="$t('子网掩码')" v-if="type === 'IPv4'" prop="netmask">
          <el-input v-emoji v-model="record.netmask" ref="mask" size='small' style="width:217px;"></el-input>
        </el-form-item>
        <el-form-item :label="$t('前缀')" v-if="type === 'IPv6'" prop="prefix">
          <el-input v-emoji v-model="record.prefix" ref="prefix" size='small' style="width:217px;"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'BondIpDialog',
  data () {
    var validateIp = (rule, value, callback) => {
      let ipv4 = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      let ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (this.type === 'IPv4' && !ipv4.test(value)) {
        callback(new Error(this.$t('IP格式不正确')))
      } else if (this.type === 'IPv6' && !ipv6.test(value)) {
        callback(new Error(this.$t('IP格式不正确')))
      } else {
        callback()
      }
    }
    var validateNetmask = (rule, value, callback) => {
      var reg = /^(254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!reg.test(value)) {
        callback(new Error(this.$t('子网掩码格式不正确')))
      } else {
        callback()
      }
    }
    var validatePrefix = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (value > 128 || value < 0) {
        callback(new Error(this.$t('请输入0-128之间的数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        ip: '',
        netmask: '',
        prefix: ''
      },
      recordCopy: {
        ip: '',
        netmask: '',
        prefix: ''
      },
      type: 'IPv4',
      rules: {
        ip: [{ required: false, validator: validateIp, trigger: 'blur' }],
        netmask: [{ required: false, validator: validateNetmask, trigger: 'blur' }],
        prefix: [{ required: false, validator: validatePrefix, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (type) {
      this.type = type
      this.dialogVisible = true
      this.$nextTick(() => {
        inputClass(this.$refs, ['ip'])
        if (type === 'IPv4') {
          inputClass(this.$refs, ['mask'])
        } else {
          inputClass(this.$refs, ['prefix'])
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('addBondIpForTable', this.record, this.type)
          this.close()
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(60vh - 184px);
  overflow-y: auto;
}
</style>
